﻿<primitives:DialogBase x:Class="Neurotoxin.Godspeed.Shell.Views.Dialogs.NewConnectionDialog"
                       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                       xmlns:i="clr-namespace:Neurotoxin.Godspeed.Presentation.Infrastructure;assembly=Neurotoxin.Godspeed.Presentation"
                       xmlns:validation="clr-namespace:Neurotoxin.Godspeed.Presentation.Validation;assembly=Neurotoxin.Godspeed.Presentation"
                       xmlns:dialogs="clr-namespace:Neurotoxin.Godspeed.Shell.Views.Dialogs"
                       xmlns:primitives="clr-namespace:Neurotoxin.Godspeed.Shell.Primitives"
                       xmlns:shellValidations="clr-namespace:Neurotoxin.Godspeed.Shell.Views.Validations"
                       xmlns:lex="http://wpflocalizeextension.codeplex.com"
                       lex:LocalizeDictionary.DesignCulture="en-US"
                       lex:ResxLocalizationProvider.DefaultAssembly="Neurotoxin.Godspeed.Shell"
                       lex:ResxLocalizationProvider.DefaultDictionary="Resources"
                       Width="500" Title="{lex:LocText ConnectionDetails}">
    <primitives:DialogBase.Resources>
        <Style TargetType="TextBox" x:Key="TextBoxStyle">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="Background" Value="Pink" />
                </Trigger>
            </Style.Triggers>
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid>
                            <Border BorderThickness="1" BorderBrush="#FFdc000c" CornerRadius="2" VerticalAlignment="Top"
                                 x:Name="border" SnapsToDevicePixels="True">
                                <Grid>
                                    <Polygon x:Name="toolTipCorner" Grid.ZIndex="2" Margin="-1" Points="6,6 6,0 0,0" Fill="#FFdc000c"
                                         HorizontalAlignment="Right" VerticalAlignment="Top" IsHitTestVisible="True" />
                                    <Polyline Grid.ZIndex="3" Points="7,7 0,0" Margin="-1" HorizontalAlignment="Right"
                                         StrokeThickness="1.5" StrokeEndLineCap="Round" StrokeStartLineCap="Round" Stroke="White"
                                         VerticalAlignment="Top" IsHitTestVisible="True" />
                                    <AdornedElementPlaceholder x:Name="adorner" Margin="-1" />
                                </Grid>
                            </Border>
                            <Popup x:Name="errorBorder" AllowsTransparency="True" IsHitTestVisible="False" IsOpen="False"
                                 PlacementTarget="{Binding ElementName=border}" Placement="Right" MinHeight="24" MaxWidth="400">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="1" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Border Background="#FFdc000c" CornerRadius="1.5" Grid.Column="1">
                                        <Border.Effect>
                                            <DropShadowEffect ShadowDepth="2.25" Color="Black" Opacity="0.4" Direction="315" BlurRadius="4" />
                                        </Border.Effect>
                                        <TextBlock Text="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                                             Foreground="White" Margin="8,3,8,3" TextTrimming="CharacterEllipsis" />
                                    </Border>
                                </Grid>
                            </Popup>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding ElementName=adorner, Path=AdornedElement.IsEnabled}"
                                 Value="False">
                                <Setter TargetName="border" Property="Visibility" Value="Hidden" />
                            </DataTrigger>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding ElementName=adorner, Path=AdornedElement.(Validation.HasError)}"
                                         Value="True" />
                                    <Condition Binding="{Binding ElementName=adorner, Path=AdornedElement.IsEnabled}"
                                         Value="True" />
                                </MultiDataTrigger.Conditions>
                                <Setter TargetName="errorBorder" Property="IsOpen" Value="True" />
                            </MultiDataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <i:DataResource x:Key="OriginalName" BindingTarget="{Binding Name}"/>
        <i:DataResource x:Key="ItemState" BindingTarget="{Binding ItemState}"/>
        <i:DataResource x:Key="ConnectionNames" BindingTarget="{Binding RelativeSource={RelativeSource AncestorType={x:Type dialogs:NewConnectionDialog}}, Path=ConnectionNames}"/>
    </primitives:DialogBase.Resources>
    <Grid x:Name="LayoutRoot" Margin="5">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="142" />
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="40" />
            <ColumnDefinition Width="50" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid Width="132" Margin="0,0,10,0" Height="132" Grid.RowSpan="5" VerticalAlignment="Top">
            <Border BorderBrush="{StaticResource NormalBrush}" BorderThickness="1" Background="White"/>
            <Image Source="{Binding Thumbnail}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="128" Height="128"/>
        </Grid>

        <TextBlock Grid.Column="1" Grid.Row="0" Text="{lex:Loc ConnectionName}" VerticalAlignment="Center" Margin="0,0,0,5" />
        <TextBox Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="3" Margin="0,0,0,5" x:Name="ConnectionName" Style="{StaticResource TextBoxStyle}">
            <TextBox.Text>
                <Binding Path="Name" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <validation:IsRequired ValidatesOnTargetUpdated="True" ErrorMessage="{lex:LocText Neurotoxin.Godspeed.Shell:Resources:EmptyFieldErrorMessage}" />
                        <shellValidations:IsNonExistentConnection ValidatesOnTargetUpdated="True" 
                                                                  ErrorMessage="{lex:LocText Neurotoxin.Godspeed.Shell:Resources:ConnectionNameExistsErrorMessage}" 
                                                                  OriginalValue="{i:DataResourceBinding DataResource={StaticResource OriginalName}}"
                                                                  ItemState="{i:DataResourceBinding DataResource={StaticResource ItemState}}"
                                                                  ConnectionNames="{i:DataResourceBinding DataResource={StaticResource ConnectionNames}}"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <TextBlock Grid.Column="1" Grid.Row="1" Text="{lex:Loc Console}" VerticalAlignment="Center" Margin="0,0,0,5" />
        <ComboBox Grid.Column="2" Grid.Row="1" Grid.ColumnSpan="3" Margin="0,0,0,5" 
                  ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type dialogs:NewConnectionDialog}}, Path=ConnectionImages}"
                  SelectedItem="{Binding ConnectionImage}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Converter={StaticResource EnumConverter}}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        
        <TextBlock Grid.Column="1" Grid.Row="2" Text="{lex:Loc Address}" VerticalAlignment="Center" Margin="0,0,0,5" />
        <TextBox Grid.Column="2" Grid.Row="2" Margin="0,0,0,5" x:Name="Address" Style="{StaticResource TextBoxStyle}">
            <TextBox.Text>
                <Binding Path="Address" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <validation:IsIPv4Address ValidatesOnTargetUpdated="True" 
                                                  EmptyErrorMessage="{lex:LocText Neurotoxin.Godspeed.Shell:Resources:IPAddressEmptyErrorMessage}" 
                                                  WrongFormatErrorMessage="{lex:LocText Neurotoxin.Godspeed.Shell:Resources:IPAddressWrongFormatErrorMessage}"
                                                  InvalidCharacterErrorMessage="{lex:LocText Neurotoxin.Godspeed.Shell:Resources:IPAddressInvalidCharacterErrorMessage}"
                                                  OctetOutOfRangeErrorMessage="{lex:LocText Neurotoxin.Godspeed.Shell:Resources:IPAddressOctetOutOfRangeErrorMessage}"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        
        <TextBlock Grid.Column="3" Grid.Row="2" Text="{lex:Loc Port}" VerticalAlignment="Center" Margin="10,0,0,5" />
        <TextBox Grid.Column="4" Grid.Row="2" Margin="0,0,0,5" x:Name="Port" Style="{StaticResource TextBoxStyle}">
            <TextBox.Text>
                <Binding Path="Port" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <validation:IsInteger ValidatesOnTargetUpdated="True" ErrorMessage="{lex:LocText Neurotoxin.Godspeed.Shell:Resources:IntegerErrorMessage}" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

        <TextBlock Grid.Column="1" Grid.Row="3" Text="{lex:Loc Login}" VerticalAlignment="Center" Margin="0,0,0,5" />
        <DockPanel Grid.Column="2" Grid.Row="3" Grid.ColumnSpan="3" Margin="0,0,0,5">
            <Button DockPanel.Dock="Right" Content="{lex:Loc Change}" Command="{Binding ChangeLoginCommand}" Margin="15,0,0,0" Padding="10,0,10,0" MinWidth="75" Height="22"/>
            <TextBlock Text="{Binding LoginInfo}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" VerticalAlignment="Center"/>
        </DockPanel>

        <TextBlock Grid.Column="1" Grid.Row="4" Text="{lex:Loc UsePassiveMode}" VerticalAlignment="Center" Margin="0,0,0,15" />
        <CheckBox Grid.Column="2" Grid.Row="4" Margin="0,0,0,15" IsChecked="{Binding UsePassiveMode}" VerticalAlignment="Center"/>

        <StackPanel Grid.ColumnSpan="5" Grid.Row="5" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,5,0">
            <Button Content="{lex:LocText Ok}" Width="75" Margin="0,0,5,0" Click="OkButtonClick" />
            <Button Content="{lex:LocText Cancel}" Width="75" Click="CancelButtonClick" />
        </StackPanel>
    </Grid>
</primitives:DialogBase>